<template>
  <div class="approve">
    <a-spin :spinning="loading">
      <div class="base-info">
        <a-form :model="formState" autocomplete="off">
          <div class="title">
            <div>
              <span style="margin-right: 16px">运单号: {{ formState.bizPreOrderShipmentResDto.waybillNum }}</span>
              <a-tag v-if="passData.state === 0" color="#2db7f5"> 待审批 </a-tag>
              <a-tag v-if="passData.state === 1" color="#f40"> 审批驳回 </a-tag>
              <a-tag v-if="passData.state === 3" color="#87d068"> 审批通过 </a-tag>
            </div>
          </div>
          <a-row :gutter="8">
            <a-col v-for="(item, index) in formItemColumn" :key="index" :span="8">
              <a-form-item
                :class="[['waybillNum', 'pickupCity', 'addressType'].includes(item.name) ? 'emphasis-item' : '']"
                v-bind="item"
                :label-col="{ style: 'width:110px' }"
                :wrapper-col="{ style: 'calc(100% - 110px)' }"
              >
                <template v-if="item.name === 'addressType'">
                  {{ getLabelByDict("WAYBILL_TYPE", formState.bizPreOrderShipmentResDto[item.name]) }}
                </template>
                <template v-else-if="item.name === 'purchaseInsurance'">
                  <span v-if="formState.bizPreOrderShipmentResDto.purchaseInsurance === 1">购买保险</span>
                  <span v-if="formState.bizPreOrderShipmentResDto.purchaseInsurance === 0">未购买保险</span>
                </template>
                <template v-else-if="item.name === 'customsDeclarationTitle'">
                  {{ formState.bizPreOrderShipmentResDto[item.name] }}
                </template>
                <template v-else-if="item.name === 'squaresNumber'">
                  {{ formState.bizPreOrderShipmentResDto[item.name] }}
                </template>
                <template v-else-if="item.name === 'volumeWeight'">
                  {{ formState.bizPreOrderShipmentResDto[item.name] }}
                  <a-button type="link" @click="showBoxInfoHandle">查看</a-button>
                </template>
                <template v-else-if="item.name === 'makeInvoiceStatus'">
                  <a-button type="link" @click="showInvoiceHandle">查看发票</a-button>
                </template>
                <template v-else-if="item.name === 'shippingMethod'">
                  <span v-if="formState.bizPreOrderShipmentResDto.shippingMethod === 0">上门取件</span>
                  <span v-if="formState.bizPreOrderShipmentResDto.shippingMethod === 1">自送入仓</span>
                </template>
                <template v-else-if="item.name === 'remark'">
                  <span :title="formState.bizPreOrderShipmentResDto.remark">{{ formState.bizPreOrderShipmentResDto.remark }}</span>
                  <!-- <a-button type="link" @click="modifyRemarkHandle">修改</a-button> -->
                </template>
                <template v-else-if="item.name === 'innerRemark'">
                  <span :title="formState.bizPreOrderShipmentResDto.innerRemark">{{ formState.bizPreOrderShipmentResDto.innerRemark }}</span>
                  <!-- <a-button type="link" @click="modifyInnerRemarkHandle">修改</a-button> -->
                </template>
                <template v-else-if="item.name === 'depositTime'">
                  <span v-if="!formState.bizPreOrderShipmentResDto.depositTime">-</span>
                  <span v-else>{{ dayjs(Number(formState.bizPreOrderShipmentResDto.depositTime)).format("YYYY-MM-DD HH:mm:ss") }}</span>
                </template>
                <template v-else>{{ formState.bizPreOrderShipmentResDto[item.name] }}</template>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 基础结算单价 -->
      <div class="unit-price-update">
        <div class="title">基础结算单价</div>
        <a-row :guuter="16">
          <a-col :span="8">
            <label for="#">标准单价: </label>
            <span>{{ formState.freightCost?.unitPrice }} 元/KG</span>
          </a-col>
          <a-col :span="8">
            <label for="#">调整单价: </label>
            <span>{{ formState.freightCost.opType === "1" ? "增加" : "减少" }} {{ formState.freightCost.adjustUnitPrice }} 元/KG</span>
          </a-col>
          <a-col :span="8">
            <label for="#">调价理由: </label>
            <span>{{ formState.freightCost.adjustReason }}</span>
          </a-col>
          <a-col :span="8">
            <label for="#">基础单价: </label>
            <span>{{ formState.freightCost.clearingUnitPrice }} 元/KG</span>
          </a-col>
        </a-row>
      </div>
      <!-- 附加费明细 -->
      <div class="additional-charge">
        <div class="title">附加费明细</div>
        <vxe-table border show-overflow="title" height="250" :data="formState.bizOrderShipmentCosts" :scroll-y="{ enabled: true, gt: 30 }">
          <vxe-column field="costName" title="费用名称" min-width="140"></vxe-column>
          <vxe-column field="unitPrice" title="单价"></vxe-column>
          <vxe-column field="number" title="数量">
            <template #default="{ row }"> {{ row.number }} {{ row.unit }} </template>
          </vxe-column>
          <vxe-column field="totalPrice" title="合计金额"></vxe-column>
          <vxe-column field="createTime" title="添加时间">
            <template #default="{ row }">
              {{ dayjs(Number(row.createTime)).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
          </vxe-column>
          <vxe-column field="createName" title="添加人"> </vxe-column>
        </vxe-table>
      </div>
      <!-- 报价合计 -->
      <div class="total-quotation">
        <div class="title">报价合计</div>
        <div class="quotation-item">
          原运费: <span class="emphasis-item">{{ formState.totalFreightPriceStr }}</span>
        </div>
        <div class="quotation-item">
          调整运费: <span class="emphasis-item">{{ formState.totalAdjustPriceStr }}</span>
        </div>
        <div class="quotation-item">
          结算费用总计: <span class="emphasis-item">{{ formState.totalPriceStr }}</span>
        </div>
      </div>
      <!-- 追加费用 -->
      <div class="additional-expense">
        <div class="title">
          <span>追加费用</span>
          <span
            >合计：<span class="emphasis-item">{{ addCostsCpt }}</span></span
          >
        </div>
        <vxe-table border show-overflow="title" height="300" :data="formState.addCosts" :scroll-y="{ enabled: true, gt: 30 }">
          <vxe-column field="costName" title="费用名称" min-width="140"></vxe-column>
          <vxe-column field="unitPrice" title="单价"></vxe-column>
          <vxe-column field="number" title="数量">
            <template #default="{ row }"> {{ row.number }} {{ row.unit }} </template>
          </vxe-column>
          <vxe-column field="totalPrice" title="金额"></vxe-column>
          <vxe-column field="adjustReason" title="追加说明"></vxe-column>
          <vxe-column field="createTime" title="添加时间">
            <template #default="{ row }">
              {{ dayjs(Number(row.createTime)).format("YYYY-MM-DD HH:mm:ss") }}
            </template>
          </vxe-column>
          <vxe-column field="createName" title="添加人"> </vxe-column>
          <vxe-column field="approvalState" title="审核状态" min-width="80">
            <template #default="{ row }">
              <a-tag v-if="row.approvalState === 0" color="#2db7f5"> 待审批 </a-tag>
              <a-tag v-if="row.approvalState === 1" color="#f40"> 审批驳回 </a-tag>
              <a-tag v-if="row.approvalState === 2" color="#ccc"> 撤回 </a-tag>
              <a-tag v-if="row.approvalState === 3" color="#87d068"> 审批通过 </a-tag>
            </template>
          </vxe-column>
        </vxe-table>
      </div>
    </a-spin>
    <!-- 操作按钮 -->
    <div class="operation-btn">
      <a-button v-if="passData.state === 0" style="margin-right: 8px" type="danger" @click="rejectHandle">驳回审批</a-button>
      <a-button v-if="passData.state === 0" style="margin-right: 8px" type="primary" @click="resolveHandle">通过并审批下一个</a-button>
      <a-button @click="emit('update:visible', false)">关闭</a-button>
    </div>
    <!-- 弹窗 -->
    <a-modal v-model:visible="dialogOpts.visible" :maskClosable="false" :keyboard="false" :closable="false" :footer="null" :title="dialogOpts.title" :width="dialogOpts.width">
      <component v-model:visible="dialogOpts.visible" :is="dialogOpts.component" :type="dialogOpts.type" :pass-data="dialogOpts.passData" @submit="dialogSubmitHandle"></component>
    </a-modal>
    <!-- 抽屉 -->
    <a-drawer class="custom-drawer" v-model:visible="drawerOpts.visible" :title="drawerOpts.title" :maskClosable="false" :closable="true" :width="drawerOpts.width" placement="right">
      <component v-model:visible="drawerOpts.visible" :is="drawerOpts.component" :type="drawerOpts.type" :pass-data="drawerOpts.passData" @submit="drawerSubmitHandle"></component>
    </a-drawer>
  </div>
</template>
<script lang="ts">
import ShowInvoice from "@/views/waybillManagement/businessWaybill/waybillQuotation/components/showInvoice/index.vue";
import ModifyRemark from "@/views/waybillManagement/businessWaybill/waybillQuotation/components/modifyRemark/index.vue";
import BoxInfo from "@/views/waybillManagement/businessWaybill/waybillQuotation/components/boxInfo/index.vue";
import RejectReason from "../rejectReason/index.vue";
export default {
  components: {
    ShowInvoice,
    ModifyRemark,
    BoxInfo,
    RejectReason
  }
};
</script>
<script lang="ts" setup>
import dayjs from "dayjs";
import CommonHook from "@/hook/commonHook";
import ModalHook from "@/hook/modalHook";
import InitHook from "./composables/initHook";
import OperationHook from "./composables/operationHook";
import { formItemColumn } from "./data";
import { toRefs, watch } from "vue";
const props = withDefaults(
  defineProps<{
    visible: boolean;
    passData: QuotationApproval.ListRows;
    searchData: () => QuotationApproval.SearchParams;
  }>(),
  {}
);
const emit = defineEmits<{
  (e: "update:visible", blo: boolean): void;
  (e: "submit"): void;
  (e: "reject"): void;
}>();
const { visible, passData, searchData } = toRefs(props);
const { getLabelByDict } = CommonHook();
const { formState, loading, dialogOpts, drawerOpts, init, addCostsCpt, search } = InitHook({ emit, searchData });
const { showInvoiceHandle, showBoxInfoHandle, rejectHandle, dialogSubmitHandle, resolveHandle } = OperationHook({
  drawerOpts,
  dialogOpts,
  passData,
  formState,
  init,
  emit,
  loading,
  search
});
const { drawerSubmitHandle } = ModalHook({
  drawerOpts,
  callBack: () => {
    init(formState.value.bizOrderShipmentQuotation.id);
  }
});
watch(
  () => visible.value,
  (v: boolean) => {
    if (v) {
      init(passData.value.orderShipmentQuotationId as unknown as string);
    }
  }
);
init(passData.value.orderShipmentQuotationId as unknown as string);
</script>
<style lang="stylus" scoped>
.approve{
    padding-bottom: 60px;
    .title{
        font-weight: 600;
        font-size: 14px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 12px;
    }
    .base-info{
      background-color: #fff;
      padding: 12px;
      border-radius: 8px;
    }
    .unit-price-update{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
      label{
        font-weight: 600;
        padding-right: 16px;
      }
      .adjust-unit-price{
        >div{
          display: flex;
          align-items: center;
          flex-wrap: nowrap;
          white-space: nowrap;
          margin-right: 16px;
        }
      }
      >div {
        display: flex;
        align-items: center;
        font-weight: 600;
      }
      :deep(.ant-col){
        margin-bottom: 12px;
      }
    }
    .additional-charge{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
    }
    .additional-expense{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
    }
    .total-quotation{
      background-color: #fff;
      padding: 12px;
      margin-top: 24px;
      border-radius: 8px;
      >.quotation-item{
        font-weight: 600;
        margin-bottom: 12px;
      }
    }
    .operation-btn{
      position: absolute;
      background-color: #fff;
      bottom: 0;
      left: 0;
      padding: 12px;
      width: 100%;
      font-weight: 600;
      text-align: center;
      z-index: 999;
      box-shadow: 0px 0px 5px #ccc;
    }
    .emphasis-item {
        color: #fe8100;
        > :deep(.ant-form-item-label){
            >label{
                color: #fe8100;
            }
        }
    }
}
</style>
